#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import sys
import os
sys.path.append(os.path.abspath(os.getcwd()))
from model.dictionary import Dictionary
import matplotlib.pyplot as plt
import numpy as np

ROUND_COUNT = 1000
WORD_COUNT = 100

Count = {}
for i in range(ROUND_COUNT):
    dict_ = Dictionary.get_dict(None)
    for word in dict_.get_words(WORD_COUNT, []):
        if word in Count:
            Count[word] += 1
        else:
            Count[word] = 1
    Dictionary.invalidate()

min_cnt = 10 ** 9
max_cnt = 0
lst = []

for i in Count:
    lst.append(Count[i])
    min_cnt = min(min_cnt, Count[i])
    max_cnt = max(max_cnt, Count[i])

plt.hist(lst)
plt.savefig('tools/random_test_invalidate.jpg')

ouf = open('tools/top50.txt', 'w')
ouf.write('min = %d, max = %d\n' % (min_cnt, max_cnt))
lst = []

for i in Count:
    lst.append((Count[i], i))

word_cnt = 0
for i, j in reversed(sorted(lst)):
    word_cnt += 1
    if word_cnt > 50:
        break
    ouf.write('%s %d \n' % (j.encode('utf-8'), i))
